package com.hhd.pdf.component.extField;

import com.hhd.pdf.CommonUtils;
import com.hhd.pdf.component.AbstractPdfComponent;
import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.hhd.pdf.variable.CompositeFormula;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ExtFieldComponent extends AbstractPdfComponent {
    @Autowired
    ExtFieldAdapter extFieldAdapter;

    @Override
    public String getName() {
        return "ext-field";
    }

    @Override
    public List<com.itextpdf.text.Element> doGenElements(GeneratorContext context) {
        String dataId = getDataId(context);
        if (CommonUtils.isEmpty(dataId)) return CommonUtils.newList();
        PdfPTable table = getTable(context);

        List<PdfPCell> res = CommonUtils.newList();

        List<ExtFieldVo> extFields = extFieldAdapter.getExtFields(dataId, context.getGeneratorParameters().getFormType(), context.getVariableContext());
        if (CommonUtils.isEmpty(extFields)) return CommonUtils.newList();

        resolveDefaultPadding(context);

        for (ExtFieldVo extField : extFields) {
            res.add(createExtFieldCell(extField, context));
        }
        res.forEach(table::addCell);
        return CommonUtils.newList(table);
    }

    private void resolveDefaultPadding(GeneratorContext context) {
        context.getParsingElement().addAttribute("paddingLeft", "30");
    }

    private PdfPTable getTable(GeneratorContext context) {
        return PdfUtils.createTable(getColumnNum(context), context.getGeneratorParameters().getPageWidth());
    }

    protected PdfPCell createExtFieldCell(ExtFieldVo extField, GeneratorContext context) {
        PdfPCell cell = PdfUtils.createCell();
        cell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
        cell.addElement(new Phrase(extField.getString(), context.getGeneratorParameters().getFont()));
        cell.setMinimumHeight(25);
        return cell;
    }

    private int getColumnNum(GeneratorContext context) {
        String colNum = context.getParsingElement().attributeValue("colNum");
        try {
            return Integer.parseInt(colNum);
        } catch (Exception e) {
        }
        return 2;
    }

    protected String getDataId(GeneratorContext context) {
        String dataIdFormulaStr = context.getParsingElement().attributeValue("data-id");
        if (CommonUtils.isEmpty(dataIdFormulaStr)) {
            dataIdFormulaStr = context.getParsingElement().attributeValue("ext-data-id");
        }
        if (CommonUtils.isEmpty(dataIdFormulaStr)) return null;
        CompositeFormula formula = CompositeFormula.formula(dataIdFormulaStr);
        formula.execute(context.getVariableContext());
        return formula.joinFormulas();
    }
}
